<?php
/*
 * export控制器  用于导出excel数据的
 * Tobey
 * */
require_once "baseController.class.php";
class exportController extends baseController{

    /*
    导出某个班级所有学生的成功
    */
    public function exportAllGrade(){
        $check = $this->classPower();
        if($check["status"] == 1){
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  //2007
            header('Content-Disposition:attachment;filename='."学生成绩表".date("Y-m-d",time()).".xls");
            header('Pragma:no-cache');
            header('Expires:0');
            $cla_id = $_GET["cad"];     //获取班级的id
            $sql = D();     //开启连接
            //获取到所有班级学生的基本信息
            $student = $sql->table(array("class_student"=>"cs","student"=>"s"))->where(array("cs.class_cla_id"=>$cla_id,"cs.is_pass"=>1,"__string__"=>"cs.student_stu_id = s.stu_id"))->field(array("s.stu_number","s.stu_name","s.stu_email","s.stu_id"))->select();          
            $quiz = $sql->table(array("question_set"=>""))->where(array("class_cla_id"=>$cla_id,"qus_status"=>array("!=",0)))->field(array("qus_id","qus_name","qus_create_time"))->order("qus_create_time desc")->select();
            $stu_num = count($student);     //班级人数
            $quiz_num = count($quiz);       //试卷数量
            for($i = 0;$i<$stu_num;$i++){
                for($j = 0;$j<$quiz_num;$j++){
                    $stu_score = $sql->table(array("student_result"=>""))->where(array("qus_id"=>$quiz[$j]["qus_id"],"stu_id"=>$student[$i]["stu_id"]))->field(array("stu_score"))->find();
                    if($stu_score){
                        $student[$i]["allResult"][$j]["stu_score"] = $stu_score["stu_score"];
                    } else{
                        $student[$i]["allResult"][$j]["stu_score"] = "未交";
                    }
                }
            }
            $this->assign("student",$student);
            $this->assign("quiz",$quiz);
            $this->display("Grade/exportAllGrade");
            unset($student,$quiz,$stu_num,$quiz_num,$sql,$cla_id);
        }
    }

    /*
    导出班级的学生的名单
    */
    public function exportClassStu(){
        $check = $this->classPower();
        if($check["status"] == 1){
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  //2007
            header('Content-Disposition:attachment;filename='."学生成绩表".date("Y-m-d",time()).".xls");
            header('Pragma:no-cache');
            header('Expires:0');
            //获取到所有班级学生的基本信息
            $student = D()->table(array("class_student"=>"cs","student"=>"s"))->where(array("cs.class_cla_id"=>$_GET["cad"],"cs.is_pass"=>1,"__string__"=>"cs.student_stu_id = s.stu_id"))->field(array("s.stu_number","s.stu_name","s.stu_email"))->select();   
            $this->assign("student",$student);
            $this->display("Grade/exportStu");
            unset($student);
        }
    }

    //析构函数不用写任何内容也是可以的
    public function __destruct(){}
}